using System.ComponentModel;

namespace Sapphire.Linq.Mappers
{
  public class TypeConverterMapper : IObjectMapper
  {
    public TTo Map<TTo>(object value, object[] parameters)
    {
      var typeConverter = TypeDescriptor.GetConverter(value.GetType());
      return (TTo)typeConverter.ConvertTo(value.GetType(), typeof(TTo));
    }

    public bool IsMatch<TTo>(object value, object[] parameters)
    {
      var typeConverter = TypeDescriptor.GetConverter(value.GetType());
      if (typeConverter != null)
        return typeConverter.CanConvertTo(typeof(TTo));

      return false;
    }
  }
}